var util = require('../../../../utils/util.js');
var DB = require('../../../../utils/db.js');
var time;
var identity;
Page({

  data: {
    name:"",
    allID: "",
    phone:"",
    time:"",
    _ID:"",
    police:"",
    inout:"-------------请选择--------------",
  },
  onLoad: function (options) {
    var _this = this;
    console.log(options)
    var allID = options.allID;
    var police = options.police;
    var inout=options.inout;
    var studenttime=options.studenttime
    this.setData({
      allID:allID,
      police:police,
      studenttime:studenttime,
      inout:inout
    })
    DB.getInfoByid(allID,function(res){
      if(res.length!=0){
        if (res[0].identity == 1) { var _ID = "本校学生";  }
        else if (res[0].identity == 2) { var _ID = "本校教工";}
        else if (res[0].identity == 3) { var _ID ="外来人员";}
        _this.setData({
          name:res[0].name,
          phone:res[0].phone,
          _ID:_ID
        })
        _this.identity = res[0].identity;
      }
    })
    DB.getStudentOppenid(allID, function (res) {
      console.log(res)
    })
    wx.showToast({
      title: '正在获取信息...',
      icon: 'loading'
    })
  },
  onShow: function(){
    //获取时间戳
    this.time = new Date();
    console.log(this.time)
    //时间戳转换
    var date = util.formatTime(this.time);
    this.setData({ time: date });

    const _this = this;
    wx.showToast({
      title: '3秒后自动提交',
      icon:'none',
      mask:true,
      duration:3000,
      success:()=>{
        setTimeout(()=>{_this.submit()},3000)
      }
    })

  },

  //点击进出触发事件
  in_or_out:function(e){
    var value = e.detail.value;
    if(value == 0){
      this.setData({ inout:"正门进校"})
    } else if(value == 1){
      this.setData({ inout: "正门出校" })
    } else if (value == 2) {
      this.setData({ inout: "南门进校" })
    } else if (value == 3) {
      this.setData({ inout: "南门出校" })
    }
  },

  //点击提交按钮触发事件
  submit:function(){
    var policeInfo = {};
    var tasktime = this.data.time;
    policeInfo.name = this.data.name;
    policeInfo.phone = this.data.phone;
    policeInfo.userid = this.data.allID;
    policeInfo.time = this.time;
    policeInfo.timestamp = Date.parse(this.time) / 1000;
    policeInfo.identity = this.identity;
    policeInfo.police = this.data.police;

    if (this.data.inout == "正门进校" || this.data.inout == "正门出校" || this.data.inout == "南门进校" || this.data.inout == "南门出校"){
    if (this.data.inout == "正门进校"){
      policeInfo.inout = 0;
      wx.showToast({
        title: '正在提交...',
        icon: 'loading',
        mask:true,
      })
    } else if(this.data.inout == "正门出校"){
      policeInfo.inout = 1;
      wx.showToast({
        title: '正在提交...',
        icon: 'loading',
        mask:true,
      })
    } else if (this.data.inout == "南门进校") {
      policeInfo.inout = 2;
      wx.showToast({
        title: '正在提交...',
        icon: 'loading',
        mask:true,
      })
    } else if (this.data.inout == "南门出校") {
      policeInfo.inout = 3;
      wx.showToast({
        title: '正在提交...',
        icon: 'loading',
        mask:true,
      })
  } 
      DB.submitInOut(policeInfo, function (res) {
        if (res == "successful") {
          DB.getStudentOppenid(policeInfo.userid, function (res) {
              wx.cloud.callFunction({
                name: 'unionid',
                data: {
                  taskopenid: res[0].openid,
                  // taskopenid:'odKVp5EG4kXO6u7emth5gfh4X8QA',
                  taskId: policeInfo.userid,
                  // taskname:policeInfo.name,
                  tasktime:tasktime,
                }
              }).then(res=>{
                console.log(res)
              })
          })
          wx.showToast({
            title: '提交成功',
            mask:true,
          })
          wx.navigateBack({})    
        } else {
          wx.showToast({
            title: '提交失败，请检查网络是否连接',
            icon: 'none',
          })
        }
      })
      
    }
    // else{
    //   wx.showToast({
    //     title: '请选择进出状态',
    //     icon: 'none'
    //   })
    // }
  }
})